<?php
namespace App\Http\Controllers\Api\V6;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;

use App\Models\V6\MessageModel;

class MessageController extends Controller{

    /*获得总的信息*/
    public function get_user_message(Request $request)
    {
        $userId = $request->input('userId');
        $messageModel   = new MessageModel();
        $messages       = $messageModel->get_user_message($userId);

        foreach($messages as &$message){

            $message->msg_type  = $message->msg_type ? $message->msg_type : "system";
        }

        $data = [
            'code'          => 200,
            'message'       => 'ok',
            'data'          => $messages
        ];
        $messageModel->read_user_message($userId);
        return response()->json($data);
    }



    /*获得未读的消息数量*/
    public function get_unreaded_message_number($userId){

        $messageModel = new MessageModel();
        $number     = $messageModel->get_unreaded_message_number($userId);
        return $number;
    }

    /**
     * 删除消息
     * */
    public function del_message(Request $request){

        $msgId  = $request->input('msgId');
        $userId = $request->input('userId');

        MessageModel::del_user_message($msgId);

        return apiData()->send();
    }
}